package com.amphora.services.crud.impl;

import java.util.HashMap;

import com.amphora.model.Category;
import com.amphora.model.SystemProperty;
import com.amphora.model.TreatmentType;
import com.amphora.services.crud.TreatmentTypeService;
import com.amphora.util.exceptions.CRUDExceptions;
import com.amphora.util.factories.DomainFactory;
import com.amphora.util.repository.JPAUtil;

public class TreatmentTypeServiceImpl implements TreatmentTypeService {

	@Override
	public void createTreatmentType(HashMap<String, String> stringValuesForTT, int treatmentTypePrice,long treatmentTypeDuration,Category category) throws CRUDExceptions {
		// TODO Auto-generated method stub
		TreatmentType newTreatmentType = DomainFactory.getTreatmentType(stringValuesForTT, treatmentTypePrice, treatmentTypeDuration);
		newTreatmentType.setCategory(category);
		
		if(JPAUtil.findById(TreatmentType.class, newTreatmentType.getId())==null)
			throw new CRUDExceptions("Error: There was a error while creating the treatment type");
	}

	@Override
	public void updateTreatmentType(TreatmentType treatmentTypeToUpdate) throws CRUDExceptions{
		// TODO Auto-generated method stub
		TreatmentType treatmentTypeToBeUpdated = JPAUtil.findById(TreatmentType.class, treatmentTypeToUpdate.getId());
		
		if(JPAUtil.findById(TreatmentType.class, treatmentTypeToUpdate.getId())==null)
			throw new CRUDExceptions("Error: There was a error while reading the treatment type from the database");
		
		JPAUtil.update(TreatmentType.class, treatmentTypeToBeUpdated.getId());
	}

	@Override
	public void deleteTreatmentType(Long treatmentTypeId) throws CRUDExceptions{
		// TODO Auto-generated method stub
		TreatmentType treatmentTypeToBeDeleted = JPAUtil.findById(TreatmentType.class, treatmentTypeId);
		
		if(JPAUtil.findById(TreatmentType.class, treatmentTypeToBeDeleted.getId())==null)
			throw new CRUDExceptions("Error: There was a error while reading the treatment type from the database");
		
		JPAUtil.delete(treatmentTypeToBeDeleted);
		
		if(JPAUtil.findById(TreatmentType.class, treatmentTypeToBeDeleted.getId())!=null)
			throw new CRUDExceptions("Error: There was a error while deleting the treatment type from the database");
	}

}
